﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;

namespace QASampling.BusinessObject
{
    class AD
    {
        internal static bool ValidateUserWithAD(string username, string password)
        {
            string domain = System.Configuration.ConfigurationManager.AppSettings["AD"];
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain))
            {
                // validate the credentials
                bool isValid = pc.ValidateCredentials(username, password);
                return isValid;
            }
        }

        /*internal static List<string> GetUserList()
        {
            string groupName = "QA_GROUP";
            string domainName = "corp.jakarta.com";
            List<string> userList = new List<string>();

            try
            {
                using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName, QAController.Username, QAController.Password))
                {
                    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, groupName);
                    if (grp != null)
                    {
                        foreach (Principal p in grp.GetMembers(false))
                        {
                            userList.Add(p.SamAccountName);
                        }

                        grp.Dispose();
                        ctx.Dispose();
                    }
                    else
                    {
                        throw new Exception("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
                    }
                }
            }
            catch (Exception ex)
            {
                return userList;
            }            

            return userList;
        }*/

    }
}
